﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace RkLibs.Security
{
    public class RkMD5
    {

        /// <summary>
        /// 获取字符串的MD5校验值
        /// </summary>
        /// <param name="str"></param>
        /// <param name="outStrLen"></param>
        /// <returns></returns>
        public static string GetMD5Hash(string str, int outStrLen)
        {
            MD5 md5Hasher = MD5.Create();
            Byte[] DataToHash = md5Hasher.ComputeHash(Encoding.Default.GetBytes(str));
            StringBuilder sBuilder = new StringBuilder();
            switch (outStrLen)
            {
                case 16:
                    for (Int16 i = 4; i < 11; i++)
                    {
                        sBuilder.Append(DataToHash[i].ToString("X2"));
                    }
                    break;
                case 32:
                    for (Int16 i = 0; i < DataToHash.Length; i++)
                    {
                        sBuilder.Append(DataToHash[i].ToString("X2"));
                    }
                    break;
                default:
                    for (Int16 i = 0; i < DataToHash.Length; i++)
                    {
                        sBuilder.Append(DataToHash[i].ToString("X2"));
                    }
                    break;
            }
            return sBuilder.ToString();
        }
    }
}
